package com.nbjtjc.safe.service.danger;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.danger.DangerInspectListMapper;
import com.nbjtjc.safe.mapper.safe.SafeBasisMapper;
import com.nbjtjc.safe.model.danger.DangerInspectList;
import com.nbjtjc.safe.model.safe.SafeBasis;
import com.nbjtjc.safe.util.MyShiroUtils;
import io.swagger.models.auth.In;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @auther guqianbin
 * @date 2018/4/9 10:12
 **/
@Service
public class DangerInspectListService {

    @Autowired
    private DangerInspectListMapper dangerInspectListMapper;

    @Autowired
    private SafeBasisMapper safeBasisMapper;

    @Transactional(rollbackFor = Throwable.class)
    public Result add(DangerInspectList dangerInspectList) {
        Result result = new Result();
        dangerInspectList.setCreateUserId(MyShiroUtils.getSessionUserId());
        dangerInspectList.setCreateTime(new Date());
        dangerInspectListMapper.insertSelective(dangerInspectList);
        return result;
    }
    @Transactional(rollbackFor = Throwable.class)
    public Result addBatch(List<DangerInspectList> dangerInspectLists) {
        Result result = new Result();
        dangerInspectLists.forEach(dangerInspectList -> {
            dangerInspectList.setCreateUserId(MyShiroUtils.getSessionUserId());
            dangerInspectList.setCreateTime(new Date());
        });
        dangerInspectListMapper.insertList(dangerInspectLists);
        return result;
    }
    @Transactional(rollbackFor = Throwable.class)
    public Result delete(DangerInspectList dangerInspectList) {
        Result result = new Result();
        dangerInspectListMapper.deleteByIds(dangerInspectList.getIds());
        return result;
    }

    @Transactional(rollbackFor = Throwable.class)
    public Result update(DangerInspectList dangerInspectList) {
        Result result = new Result();
        dangerInspectListMapper.updateByPrimaryKeySelective(dangerInspectList);
        return result;
    }

    public Result find(DangerInspectList dangerInspectList) {
        Result result = new Result();
        List<DangerInspectList> dangerInspectLists=dangerInspectListMapper.search(dangerInspectList);
        if(dangerInspectList.getNeedRowspan()){
            HashMap<String,List<DangerInspectList>> rowMap=new HashMap<>();
            dangerInspectLists.forEach(list->{
                if(!rowMap.containsKey(list.getInspectProject())){
                    List<DangerInspectList> inspectLists=new ArrayList<>();
                    inspectLists.add(list);
                    rowMap.put(list.getInspectProject(),inspectLists);
                }else{
                    rowMap.get(list.getInspectProject()).add(list);
                }
            });
            dangerInspectLists=new ArrayList<>();
            for(String key :rowMap.keySet()){
                rowMap.get(key).get(0).setRowspan(rowMap.get(key).size());
                dangerInspectLists.addAll(rowMap.get(key));
            }
            dangerInspectLists.sort(Comparator.comparing(DangerInspectList::getInspectProjectOrder).thenComparing(DangerInspectList::getNum));
        }
        if(dangerInspectList.getNeedBasis()){
            compareBasis(dangerInspectLists);
        }
        result.setData(dangerInspectLists);
        return result;
    }

    private void compareBasis(List<DangerInspectList> dangerInspectLists){
        List<Integer> inspectListIds=dangerInspectLists.stream()
                .map(DangerInspectList::getBasisId).collect(Collectors.toList());
        if(CollectionUtil.isEmpty(inspectListIds)){
            return;
        }
        List<SafeBasis> safeBasisList=safeBasisMapper.findByIds(inspectListIds);
        if(CollectionUtil.isNotEmpty(safeBasisList)){
           Map<Integer,SafeBasis> basisMap= safeBasisList.stream().collect(Collectors
                    .toMap(SafeBasis::getId,safeBasis->safeBasis));
           dangerInspectLists.forEach(inspectList->{
               if(ObjectUtil.isNotNull(inspectList.getBasisId())){
                   inspectList.setSafeBasis(basisMap.get(inspectList.getBasisId()));
               }
           });
        }
    }


    public Result findByDangerInspectIds(List<Integer> inspectIds){
        Result result = new Result();
        if(CollectionUtil.isNotEmpty(inspectIds)){
            List<DangerInspectList> dangerInspectLists=dangerInspectListMapper.findByInspectIds(inspectIds);
            result.setData(dangerInspectLists);
        }else{
            result.setData(new ArrayList<>());
        }
        return result;
    }
}
